/*
 Copyright To change this template, choose Tools | Templates
 Copyright and open the template in the editor.
 */

package com.click.app.dao.hibernate;

import com.click.app.dao.SeamStorageDao;
import com.click.app.model.Material;
import com.click.app.model.Model;
import com.click.app.model.ModelSize;
import com.click.app.model.PantDesign;
import com.click.app.model.SeamStorage;
import com.click.app.model.TradeMark;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author Joel Chuca Condo
 * @date 17/06/2009
 */
public class SeamStorageDaoHibernate extends GenericDaoHibernate<SeamStorage, Long> implements SeamStorageDao{

    public SeamStorageDaoHibernate(){
        super(SeamStorage.class);
    }

    public List<SeamStorage> findByPantDesignAndTradeMark(Long idPantDesign, Long idTrademark){
        Object[] values={idPantDesign, idTrademark};
        return getHibernateTemplate().find("from SeamStorage where pantDesign.id=? AND tradeMark.id=? ", values);
    }

    public List<PantDesign> findByTradeMark(Long idTradeMark){
        //return getHibernateTemplate().find("select ss.pantDesign from SeamStorage as ss where ss.tradeMark.id="+idTradeMark+" )");
        List<PantDesign> pantDesigns = getHibernateTemplate().find("select ss.pantDesign from SeamStorage as ss where ss.tradeMark.id="+idTradeMark+" )");
//        for (PantDesign pantDesign : pantDesigns) {
//            System.out.println(pantDesign);
//        }
        return pantDesigns;
    }

    public List<PantDesign> findByTradeMarkWithoutModelSize(Long idTradeMark){
        List<PantDesign> pantDesigns = getHibernateTemplate().find("select Distinct ss.pantDesign from SeamStorage as ss where ss.tradeMark.id="+idTradeMark+" )");
          for (PantDesign pantDesign : pantDesigns) {
            System.out.println("<> : "+pantDesign);
        }
        return pantDesigns;
    }

    public List<PantDesign> findByModelAndMaterial(Long idTradeMark){
        Model model=null;
        Material material=null;
        List<PantDesign> result = new ArrayList<PantDesign>();
        List<PantDesign> pantDesigns = getHibernateTemplate().find("select ss.pantDesign from SeamStorage as ss where ss.tradeMark.id="+idTradeMark+" )");
        for (PantDesign pantDesign : pantDesigns) {
            if( model==null && material==null ){
                model = pantDesign.getModel();
                material = pantDesign.getMaterial();
                result.add(pantDesign);
            }
            if( !(model.equals(pantDesign.getModel()) && material.equals(pantDesign.getMaterial())) ) {
                result.add(pantDesign);
                model = pantDesign.getModel();
                material = pantDesign.getMaterial();
            }
        }
        return result;

    }
    public List<SeamStorage> findByPantDesignAndTradeMarkAndSizes(PantDesign pantDesign, Long idTradeMark, Set<ModelSize> sizes){
        List<SeamStorage> result = new ArrayList<SeamStorage>();
        for (ModelSize modelSize : sizes) {
            String query = "select ss from SeamStorage as ss " +
                    " where ss.tradeMark.id="+idTradeMark+
                    " and ss.pantDesign.model.id="+pantDesign.getModel().getId()+
                    " and pantDesign.material.id="+pantDesign.getMaterial().getId()+
                    " and pantDesign.modelSize.id='"+modelSize.getId()+"'";
            List<SeamStorage> SeamStorages = getHibernateTemplate().find(query);
            if(!SeamStorages.isEmpty())
                result.add(SeamStorages.get(0));
            else{
                result.add(new SeamStorage());
            }
        }
        return result;
    }

    public List<SeamStorage> findByPantDesignAndTradeMark(Long codeMaterial, Long codeModel, Long idTradeMark, Set<ModelSize> modelSizes) {
         List<SeamStorage> result = new ArrayList<SeamStorage>();
        for (ModelSize modelSize : modelSizes) {
            String query = "select ss from SeamStorage as ss " +
                    " where ss.tradeMark.id="+idTradeMark+
                    " and ss.pantDesign.model.id="+codeModel+
                    " and pantDesign.material.id="+codeMaterial+
                    " and pantDesign.modelSize.id='"+modelSize.getId()+"'";
            List<SeamStorage> SeamStorages = getHibernateTemplate().find(query);
            if(!SeamStorages.isEmpty())
                result.add(SeamStorages.get(0));
        }
        return result;
    }

    public List<TradeMark> findEnablesTradeMark() {
        List<TradeMark> listTrademark = getHibernateTemplate().find("select distinct ss.tradeMark from SeamStorage as ss where ss.stock > 0");
        return listTrademark;
    }

}
